<?php if (!defined("_ECRIRE_INC_VERSION")) return;
/**
 * Aloha Cart
 *
 * @author     Aleksey Yaroshchuk (aleksey.yaroshchuk@gmail.com)
 * @license    GNU/GPL
 * @package    plugins
 * @subpackage alohacart
 * @copyright  (c) Aleksey Yaroshchuk 2011-2012
 * @link       http://www.spip-contrib.net/AlohaCart
 */

include_spip('functions/pdk_common');
include_spip('functions/pdk_message');

function formulaires_alohacart_order_traiter_dist($id_order){
    $order_mdl = pdk_load_class('AlohacartOrderModel','classes');
	$result = array();
    try {
        if (_request('delete_order')) {
            if ($order_mdl->delete_by_id($id_order)) {            
                pdk_set_message_ok(_T('alohacart:form_deleted'));                
            } else { 
                pdk_set_message_error(_T('alohacart:form_error'));                
            }
            include_spip('inc/headers');
            redirige_url_ecrire('alohacart_orders');
        }
        
        $data = $order_mdl->form_data_from_array($_REQUEST);                        
        if (!$order_mdl->update_by_id($id_order, $data, true))
            throw new Exception("Product save fail!");
        
        if (!$order_mdl->RecalculateOrder($id_order))
            throw new Exception("Product recalculate fail!");    
        
        $result['message_ok'] = _T('alohacart:form_saved');    
    } catch(Exception $e) {
        $result['message_erreur'] = _T('alohacart:form_error');
    }        
    
    // Order total price may be changed after recalculation, so we will clear request variable 
    set_request('total_price');
    
    return $result;
}
